<?php

/*
This db class is about 2x slower than the orgiginal mysql functions
Total time of 1.5326013565063 = 0.01533 sec Average for MySQL func
Total time of 3.6232841014862 = 0.03623 sec Average for MySQL.class.php

The test was running a for() loop 20 times for both versions, 100 times in a row.
So each type of database script was run 2000 times by the scripts end. 
Both were selecting 20 text fields from a small db.
*/



class sqlite_cx_db extends cx_db {
    
    var $error_messages = array (
                'Require $dbuser and $dbpassword to connect to a database server',
                'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?',
                'Could not select database',
                'mySQL database connection is not active',
                'Unexpected error while trying to select database',
                'Error with SQLite query',
                '<b>Fatal Error:</b> mysql.db.class requires mySQL Lib to be compiled and or linked in to the PHP engine',
                '<b>Fatal Error:</b> mysql.db.class requires db.class to be included/loaded before it can be used'
    );

    ///////////////////////////////////////////////////
    // Constructor
    function SQLITE_CX_DB($db_name) {
        
        $this->db_name  = $db_name;
        $this->db_type  = 'sqlite';
        
        if (!function_exists('sqlite_open'))
            die($this->$error_messages[5]);
	if (!class_exists('CX_DB')) 
            die($this->$error_messages[6]);
        
        //Set this as the last action
        $this->last_action = 'Construct SQLITE_CX_DB';
        
        //Try to connect now
        $this->connect();
        
        return true;
    }


    ///////////////////////////////////////////////////
    // Connect to a SQLite DB
    function connect($db_name, $db_persistent) {
    
        //If no file was given -use class default
        if ($db_name) { $this->db_name = $db_name; }
        
        //Set the type of connection
        if ($db_persistent) {$this->db_persistent = $db_persistent; }
        
        $sqlite_error = null;
        
        //If it is a persistent connection (keeps running after script ends)
        if($this->db_persistent) {
            
            // Try to establish the persistent server database handle
            $this->db_handle = @sqlite_open($this->db_name, 0666, $sqlite_error);
            
            //Set this as the last action
            $this->last_action = 'sqlite_open($this->db_name, 0666, $sqlite_error)';
            
        } else {
        
            // Try to establish the persistent server database handle
            $this->db_handle = @sqlite_popen($this->db_name, 0666, $sqlite_error);
            
            //Set this as the last action
            $this->last_action = 'sqlite_popen($this->db_name, 0666, $sqlite_error)';
        }
        
        
        //If false (error on DB connect)
        if(!$this->db_handle) {
            //Add the error
            $this->register_error(sqlite_error_string(sqlite_last_error($this->db_handle)). $sqlite_error, $this->error_messages[1].' in '. basename(__FILE__). ' on line '.__LINE__);
        }
        
        //The return the resource ID of the connection
        return $this->db_handle;
    }

    ///////////////////////////////////////////////////
    // Close a SQLite connection
    function close($db_handle) {
    
        if(is_resource($db_handle)) { $this->db_handle = $db_handle; }
        
    	// Closing connection
        sqlite_close($this->db_handle);
        
        //Set this as the last action
        $this->last_action = 'sqlite_close($this->db_handle)';
    
    }

    ///////////////////////////////////////////////////
    // Select a SQLite DB
    function select($db_name, $db_persistent) {
        return $this->connect($db_name, $db_persistent);
    }


    ///////////////////////////////////////////////////
    // Select a DB - over-ridden by specific DB class
    function database($action, $db_name) {
    	
        if ($db_name) { $this->db_name = $db_name; }
        
        if($action = 'create') {
            return $this->query("CREATE DATABASE ". $this->db_name);
        } else if($action = 'drop') {
            return $this->query("DROP DATABASE ". $this->db_name);
        }
    }


    ///////////////////////////////////////////////////
    // Basic MySQL Query
    function query($query, $buffer=true) {
    	
        // Increase the query count
        $this->total_queries++;
        
        $sqlite_error = null;
        
        // If there is no existing database connection then try to connect
        if (!$this->db_handle) {
            $this->connect();
        }
        
        if($buffer) {
            // Perform a buffered query
            $this->result = @sqlite_query($query, $this->db_handle);
        } else {
            // Perform an unbuffered query
            $this->result = @sqlite_unbuffered_query($query, $this->db_handle);
        }
        
        //Set this as the last action
        $this->last_action = 'sqlite_query('. $query. ', $this->db_handle)';
        
        if(!$this->result) {
            //Add the error
            $this->register_error(sqlite_error_string(sqlite_last_error($this->db_handle)), $this->error_messages[5].' in '.basename(__FILE__).' on line '.__LINE__);
        }
        
        return $this->result;
        
    }


    ///////////////////////////////////////////////////
    // MySQL result fetch
    function fetch($type, $result) {
    
        //If a result was passed use it instead of the last result
        if(is_resource($result)) { $this->result = $result; }
        
        //Set this as the last action
        $this->last_action = 'fetch($type, $result)';
        
        if($type == 'object') {
            return @sqlite_fetch_object($this->result);
        } elseif ($type == 'row') {
            return @sqlite_fetch_array($this->result, SQLITE_NUM);
        } else {
            return @sqlite_fetch_array($this->result, SQLITE_ASSOC);
        }
    }


    ///////////////////////////////////////////////////
    // Fetch a single result - like the result of COUNT(*)
    function fetch_value($result) {
    	
        //If a result was passed use it instead of the last result
        if(is_resource($result)) { $this->result = $result; }
        
        return @sqlite_fetch_single($this->result);
        
    }
    

    ///////////////////////////////////////////////////
    // Number of DB rows affected by last query
    function affected_rows($result) {

        //If a result was passed use it instead of the last result
        if(is_resource($result)) { $this->result = $result; }
        
        return @sqlite_changes($this->result);
        
    }


    ///////////////////////////////////////////////////
    // Number of DB rows found by last query
    function found_rows($result) {
    
        //If a result was passed use it instead of the last result
        if(is_resource($result)) { $this->result = $result; }
        
        return @sqlite_num_rows($this->result);
    }


    ///////////////////////////////////////////////////
    // ID generated from the previous INSERT operation
    function last_row_id($result) {
    
        //If a result was passed use it instead of the last result
        if(is_resource($result)) { $this->result = $result; }
        
    	return @sqlite_last_insert_rowid($this->result);
    }
    

    ///////////////////////////////////////////////////
    // Format a string correctly for safe insert (shouldn't be used)
    function escape($value) {
    	return @sqlite_escape_string($value);
    }

}

